// view/index/bossquan/bossquan.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: yuming,
    heights: "",
    title: '区域社群',
    huan: [{
      name: '广场',
      id: '1'
    }, {
      name: '关注',
      id: '2'
    }, {
      name: '我的',
      id: '3'
    }],
    currtab: 0,
    photo: yuming + 'static/applet/images/my/mybj.png',
    xiao: [],
    category: '', //1广场，2已关注的，3我的
    avatar: '',
    nickname: '',
    // shopBosscircle  动态 shopfollow  关注   shopCoverfollow  粉丝
    bossBackg: '', //上传之后的背景图
    dian: false, //是否点赞
    shopId: wx.getStorageSync('shopId'),
    page: 1,
    idss: ''
  },
  // 删除员工
  delete(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showModal({
      title: '您确定要删除该动态吗？',
      cancelText: '否',
      confirmText: '是',
      success(res) {
        if (res.confirm) {
          console.log("是")
          that.shan(id)
        } else {
          console.log("否")
        }
      }
    })
  },
  shan(id) {
    var that = this
    wx.request({
      url: yuming + 'shop/shopDelBossCircle',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        id: id,
        shopId: wx.getStorageSync('shopId')
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          that.liebiao()
        }
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 点赞
  dianzan(e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.request({
      url: yuming + 'shop/shopPubBossCircle',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        shopId: wx.getStorageSync('shopId'),
        id: id
      },
      success: function (res) {
        // wx.showToast({
        //   title: res.data.msg,
        //   icon: 'none'
        // })
        // setTimeout(function(){
        if (res.data.code == 200) {
          that.liebiao()
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }

        // },1000)
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 关注
  yiguan() {
    var that = this
    wx.request({
      url: yuming + 'shop/shopAddBossfollow',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        shopId: wx.getStorageSync('shopId'),
        fid: that.data.shop.id
      },
      success: function (res) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        that.liebiao()
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 查看更多
  lookmore() {
    shuzong = this.data.xiao
    this.setData({
      page: (this.data.page) + 1
    })
    this.liebiao()
  },
  // 列表
  liebiao() {
    // wx.showLoading({
    //   title: '加载中',
    // })
    var that = this
    wx.request({
      url: yuming + 'shop/shopSelectBossCircle',
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token'),
        source: 'EabEnd'
      },
      data: {
        page: that.data.page,
        shopId: that.data.idss,
        category: 3
      },
      success: function (res) {
        wx.hideLoading({})
        shu = res.data.data
        if (that.data.page != 1) {
          if (shu.length == 0) {
            wx.showToast({
              title: '没有更多数据了~',
              icon: "none"
            })
            that.setData({
              xiao: []
            })
          }
        }
        that.setData({
          xiao: shuzong.concat(shu),
          avatar: res.data.user.avatar,
          nickname: res.data.user.nickname,
          shop: res.data.shop,
          shopfollow: res.data.shopfollow,
          shopBosscircle: res.data.shopBosscircle,
          shopCoverfollow: res.data.shopCoverfollow,
          bossBackg: res.data.shop.bossBackg
          // shopBosscircle  动态 shopfollow  关注   shopCoverfollow  粉丝
        })
      },
      fail: function () {
        app.consoleLog("请求数据失败");
      }
    })
  },
  // 放大
  fangda(e) {
    console.log(e)
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // boss详情
  bossxq(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/page_two/bossxq/bossxq?id=' + id,
    })
  },
  chuan(e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: yuming + 'Besall/Upload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {

          },
          success(res) {
            var data = JSON.parse(res.data)
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
            var urls = data.httpUrl + data.imgUrl
            that.cropper.init({
              imgPath: urls, //imgPath是需要裁剪图片的图片路径，只支持本地或临时路径
              success(res) {
                console.log(res) //res即裁剪成功后的图片临时路径
                wx.uploadFile({
                  url: yuming + 'Besall/Upload',
                  filePath: res,
                  name: 'file',
                  formData: {

                  },
                  success(res) {
                    var datas = JSON.parse(res.data)
                    // wx.showToast({
                    //   title: datas.msg,
                    //   icon: 'none'
                    // })
                    var urlsa = datas.httpUrl + datas.imgUrl
                    that.setData({
                      photo: urlsa,
                      bossBackg: datas.imgUrl
                    });
                    wx.request({
                      url: yuming + 'shop/shopSaveBossBg',
                      method: 'POST',
                      header: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                        token: wx.getStorageSync('token'),
                        source: 'EabEnd'
                      },
                      data: {
                        shopId: wx.getStorageSync('shopId'),
                        bossBackg: that.data.bossBackg
                      },
                      success: function (res) {
                        wx.showToast({
                          title: res.data.msg,
                          icon: 'none'
                        })
                        that.liebiao()
                      },
                      fail: function () {
                        app.consoleLog("请求数据失败");
                      }
                    })
                  }
                });
              },
              fail(error) {
                console.log(error) //有两种:cancel代表点击了叉，fail代表wx.canvasToTempFilePath生成图片失败
              }
            });
          }
        })
      }
    })
  },
  // 发布动态
  fabu() {
    wx.navigateTo({
      url: '/page_two/fadt/fadt?avatar=' + this.data.avatar + "&nickname=" + this.data.nickname,
    })
  },
  // tab切换
  qies(e) {
    var index = e.currentTarget.dataset.currtab
    var id = e.currentTarget.dataset.id
    this.setData({
      currtab: index,
      category: id,
      page: 1,
      xiao: []
    })
    shuzong = []
    shu = []
    this.liebiao()
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      idss: options.id
    })
    this.setData({
      category: this.data.huan[0].id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.cropper = this.selectComponent("#yeyouzi-cropper");

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.liebiao()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})